//
//  GoodNameCell.m
//  Wujin
//
//  Created by 东 on 16/12/8.
//  Copyright © 2016年 dong. All rights reserved.
//

#import "GoodNameCell.h"

@implementation GoodNameCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    [self layoutIfNeeded];
    
    _count = 1;
    
    _orderGoodsModel = [[OrderGoodsModel alloc] init];
    
    _subBtn.layer.cornerRadius = 5;
    _subBtn.layer.masksToBounds = YES;
    _addBtn.layer.cornerRadius = 5;
    _addBtn.layer.masksToBounds = YES;
    
    _countField.text = @"1";
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setModel:(GoodsModel *)model
{
    _model = model;
    
    _nameLab.text = model.name;
    _priceLab.text = F(@"%@", model.price);
    _unitLab.text = F(@"单位：%@", model.unit);
    
    [self setSubButtonWith:_colorLab andArray:model.colors];
    [self layoutIfNeeded];
    [self setSubButtonWith:_guigeLab andArray:model.models];
    
    _orderGoodsModel.Name = _model.name;
    _orderGoodsModel.Url = _model.gds_img;
    _orderGoodsModel.Count = @"1";
    _orderGoodsModel.Gds_id = F(@"%@", _model.gds_id);
}

- (void)setSubButtonWith:(UIView *)view andArray:(NSArray *)arr
{
    CGFloat maxX = CGRectGetMaxX(view.frame);
    CGFloat midY = CGRectGetMidY(view.frame);
    
    CGFloat margin = 5;
    CGFloat width = (self.bounds.size.width - maxX - 15) / 3;
    CGFloat height = 20;
    
    CGFloat y = (view == _guigeLab) ? midY - 10 : midY + 10;
    
    if (arr.count == 0) {
        
        self.maxHeight = CGRectGetMaxY(_subBtn.frame) + 5;
        _price = [self.model.price floatValue];
        return;
    }
    
    for (int i =0 ; i < arr .count; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        int y_xishu = i / 3;
        CGFloat x = maxX + (margin + width) *(i % 3);
        CGFloat btnY = y + (height + 5 ) * y_xishu;
        
        btn.frame = CGRectMake(x, btnY, width, height);
        
        btn.layer.borderWidth = 1;
        btn.layer.cornerRadius = 5;
        btn.layer.masksToBounds = YES;
        btn.layer.borderColor = MAIN_GRAY_COLOR.CGColor;
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        btn.tag = (view == _colorLab) ? 100 +i : 200+i;

        [self.contentView addSubview:btn];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:MAIN_GRAY_COLOR forState:UIControlStateNormal];
        [btn setTitleColor:skin.skinColor forState:UIControlStateSelected];
        
        if (view == _colorLab)
        {
            [btn addTarget:self action:@selector(selectColorWithBtn:) forControlEvents:UIControlEventTouchUpInside];
            if (i==0)
            {
                [self selectColorWithBtn:btn];
            }
        }
        else if (view == _guigeLab)
        {
            [btn addTarget:self action:@selector(selectGuigeWithBtn:) forControlEvents:UIControlEventTouchUpInside];
            if (i==0)
            {
                [self selectGuigeWithBtn:btn];
            }
        }
    }
    
    if (view == _colorLab)
    {
        _guigeY.constant = 8 + (25 * (arr.count / 4));
    }
    if (view == _guigeLab)
    {
        NSInteger tag = 200 + arr.count - 1;
        [self viewWithTag:tag handler:^(UIView *view, BOOL *stop) {
            self.maxHeight = CGRectGetMaxY(view.frame) + 45;
            //DLog(@"self.maxHeight===%lf", self.maxHeight);
        }];
    }
}

- (void)selectColorWithBtn:(UIButton *)btn
{
    _selectColorBtn.selected = NO;
    _selectColorBtn.layer.borderColor = MAIN_GRAY_COLOR.CGColor;
    btn.selected = YES;
    _selectColorBtn = btn;
    _selectColorBtn.layer.borderColor = skin.skinColor.CGColor;
    
    [self changePrice];
}

- (void)selectGuigeWithBtn:(UIButton *)btn
{
    _selectGuigeBtn.selected = NO;
    _selectGuigeBtn.layer.borderColor = MAIN_GRAY_COLOR.CGColor;
    btn.selected = YES;
    _selectGuigeBtn = btn;
    _selectGuigeBtn.layer.borderColor = skin.skinColor.CGColor;

    [self changePrice];
    
    _orderGoodsModel.Type = F(@"%@+%@", _selectColorBtn.titleLabel.text, _selectGuigeBtn.titleLabel.text);
    _orderGoodsModel.Price = F(@"%.2lf", _price);
}

- (void)changePrice
{
    for (NSDictionary *dict in _model.type)
    {
        BOOL sameColor = [_selectColorBtn.titleLabel.text isEqualToString:dict[@"color"]];
        BOOL sameGuige = [_selectGuigeBtn.titleLabel.text isEqualToString:dict[@"model"]];
        
        if (sameColor && sameGuige)
        {
            _priceLab.text = dict[@"price"];
            _price = [dict[@"price"] floatValue];
            
            [self priceChangeCallBack];
            break;
        }
    }
}

- (IBAction)addOrSubCount:(UIButton *)btn
{
    
    NSInteger currentCount = [_countField.text integerValue];
    
    NSInteger para = (btn == _addBtn) ? 1 : -1;
    
    NSInteger changeCount = currentCount + para;
    
    changeCount = changeCount < 1 ? 1 : changeCount;
    _count = changeCount;
    _countField.text = F(@"%ld", (long)changeCount);
    
    [self priceChangeCallBack];
}

- (void)textFieldDidChange
{
    _count = [_countField.text integerValue];
    [self priceChangeCallBack];
}

- (void)priceChangeCallBack
{
    if (_priceChangeBlock)
    {
        _totalPrice = _price * _count;
        
        NSString *color = _selectColorBtn.titleLabel.text;
        NSString *guige = _selectGuigeBtn.titleLabel.text;
        _orderGoodsModel.Type = F(@"%@+%@", color, guige);
        _orderGoodsModel.Price = F(@"%.2lf", _price);
        _orderGoodsModel.Count = F(@"%.0lf", _count);
        _orderGoodsModel.TotalPrice = F(@"%.2lf", _totalPrice);
        
        _priceChangeBlock(_orderGoodsModel);
    }
}

@end
